package com.bdqn.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bdqn.demo.pojo.User;
import com.bdqn.demo.service.UserService;
import com.bdqn.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/userInfo")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getPage/{pageNo}/{pageSize}")
    public Result getPage(String username, @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        IPage<User> page = userService.userByUsername(username, pageNo, pageSize);
        return Result.ok("true", page);
    }

    @Autowired
    PasswordEncoder passwordEncoder;

    @PostMapping("/create")
    public Result create(@RequestBody User user) {
        user.setPassword( passwordEncoder.encode(user.getPassword()) );
        boolean result = userService.save(user);
        return result ? Result.ok("添加成功") : Result.fail("数据有问题");
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        user.setPassword( passwordEncoder.encode(user.getPassword()) );
        boolean result = userService.updateById(user);
        return result ? Result.ok("修改成功") : Result.fail("数据有问题");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        boolean result = userService.deleteById(id);
        return result ? Result.ok("删除成功") : Result.fail("数据有问题");
    }


}
